package com.mfhcd.facepay;

import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.alibaba.android.arouter.launcher.ARouter;
import com.landi.finance.face.aidl.FaceService;
import com.landi.finance.face.aidl.constant.Constant;
import com.mfhcd.facepay.global.AppConfig;
import com.mfhcd.facepay.utils.LogUtil;

public class App extends Application {
    private static Context mContext;
    private static App mApp;
    private static FaceService mFaceService;
    private ServiceConnection mConnService = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder service) {
            //服务绑定成功，FaceService 中为人脸识别 sdk 所提供的接口方法
            mFaceService = FaceService.Stub.asInterface(service);
            LogUtil.d("服务绑定成功");
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            //服务解绑，服务解绑后请根据业务场景确认是否需要重新发起绑定。
            mFaceService = null;
            LogUtil.d("服务绑定失败");
        }
    };

    public static App getInstance() {
        return mApp;
    }

    public static Context getContext() {
        return mContext;
    }

    public static FaceService getFaxeService() {
        return mFaceService;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
        mApp = this;

        if (AppConfig.DEBUG) {
            // 打印日志
            ARouter.openLog();
            // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
            ARouter.openDebug();
        }
        ARouter.init(this);

        bindFacePay();
    }

    /**
     * 绑定刷脸支付服务
     */
    public void bindFacePay() {
        Intent intent = new Intent();
        intent.setPackage(Constant.SERVICE_PACKAGE);
        intent.setAction(Constant.SERVICE_ACTION);
        bindService(intent, mConnService, Context.BIND_AUTO_CREATE);
    }
}
